package com.example.yangjintingtest.controller;

import com.example.yangjintingtest.domain.ArticleResponseData;
import com.example.yangjintingtest.domain.BlogComment;
import com.example.yangjintingtest.domain.User;
import com.example.yangjintingtest.mapper.CommentMapper;
import com.example.yangjintingtest.service.CommentService;
import com.example.yangjintingtest.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class BlogCommentController {
    @Autowired
    CommentService commentService;
    // 发表评论操作
    @PostMapping(value = "/comments/publish")
    @ResponseBody
    public ArticleResponseData publishComment (HttpServletRequest request, @RequestParam Integer aid, @RequestParam String text) {
// 去除 js 脚本
        text = MyUtils.cleanXSS (text);
        text = EmojiParser.parseToAliases (text);
// 获取当前登录用户
        HttpSession session = request.getSession ();
        User user = (User) session.getAttribute ("user");
// 封装评论信息
        BlogComment comments = new BlogComment ();
        comments.setArticleId (aid);
        comments.setIp (request.getRemoteAddr ());
        comments.setCreated (new Date());
        comments.setAuthor (user.getUserName ());
        comments.setContent (text);
        try {
            commentService.pushComment (comments);
            logger.info("发布评论成功，对应文章 id:"+aid);
            return ArticleResponseData.ok ();
        } catch (Exception e) {
            logger.error ("发布评论失败，对应文章 id:"+aid +"; 错误描述:"+e.getMessage ());
            return ArticleResponseData.fail ();
        }
    }
    @Autowired
    CommentMapper commentMapper;
    // 记录系统操作日志
    private static final Logger logger = LoggerFactory.getLogger (BlogCommentController.class);
    @PostMapping ("/admin/comment/delete")
    @ResponseBody
    public ArticleResponseData deleteComment (int id) {
        try {
            commentMapper.deleteComment (id);
            logger.info("文章删除成功");
            return ArticleResponseData.ok ();
        } catch (Exception e) {
            logger.error ("文章删除失败，错误信息:"+e.getMessage ());
            return ArticleResponseData.fail ();
        }
    }
    // 跳转到后台评论列表页面
    @GetMapping(value = "/admin/comments")
    public String index (@RequestParam (value = "page", defaultValue = "1") int page,
                         @RequestParam (value = "count", defaultValue = "10") int count,
                         HttpServletRequest request) {
        PageHelper.startPage (page, count);
        List<BlogComment> commentList = commentMapper.selectComment();
        PageInfo<BlogComment> pageInfo=new PageInfo<>(commentList);
        request.setAttribute("comments", pageInfo);
        return "back/comment_list";
    }
}
